<?php
include('../Connections/KRAPPERCON.php');

$cbo_tipreg = $_POST['cbo_tipreg'];
$t_tipreg = $_POST['t_tipreg'];
if (isset($_POST['migo'])){
$migo = $_POST['migo'];
}
else {
$migo = ' ';
}
// Obtenemos la variable combo que tiene '<option>
include ('getAlmacenes.php');
//echo $combo;


if ($migo == ' '){

$moneda = $_POST['moneda'];

	$cst_ind = $_POST['cstind'];
	$ocs[] = array();
	$new_sql = '';
	$sql = "call sp_getDocXFacturar('".$cbo_tipreg."','".$t_tipreg."');";
	$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
	if ($res){		
		while($row = $res->fetch_assoc()){
		if ( array_search($row['OrdenNro'],$ocs) == 0 ){
		$ocs[] = $row['OrdenNro'];
		$new_sql=$new_sql.",'".$row['OrdenNro']."'";
		}		
		if ($row['MoneyID']!=$moneda){
		continue;
		}
		
		$qt_pen = $row['DetOrdQta']-$row['DetOrdQtf'];  // Mostrar solo la ctd Pendiente
		
	?>
		  <tr class="trFob">
			 <td><input type="checkbox" checked='checked' class="clsFacChk" onchange="calcDetFac()" /></td>
			 <td><a href="#" onclick="verOC(this)"><?php echo $row['OrdenNro']; ?></a></td>
			 <td><?php echo $row['OrdenPos']; ?></td>
			<td><?php echo $row['ArticuloID']; ?></td>
			<td class="clsTxtPos"><?php echo $row['DetOrdTxt']; ?></td>
			<td class="qt clsFcPru"><?php echo myRound($row['DetPreUM'] * ( 1 - $row['DetOrdDct']  * 0.01))  ;?></td>
            <td class="qt clsFcMon"><?php echo $row['MoneyID'] ;?></td>
			<td><input class="qt clsQtPen" type="text"  onchange="mr(this);calcDetFac()" value="<?php echo $qt_pen ;?>" /></td>
<!-- 			<td class="qt clsFcDct"><?php echo $row['DetOrdDct'] ;?></td>        -->
			<td class="qt clsFcNet"><?php echo $row['DetPreNet'];?></td>
			<td><select class="clsFacAlm" disabled="disabled" class="disabled">
			<?php //echo $combo;
			echo str_replace("value='".$row['AlmacenID']."'","value='".$row['AlmacenID']."' selected='selected'",$combo); ?>
			</select></td>       
			</tr>
	<?php 	
		}
		$res->close();
		
			$mysqli->check_result();
		
		//doEcho($new_sql);
		if ($cst_ind == 'X' && $new_sql != "" ){	
			$new_sql = "select a.OrdenNro,a.OrdenPos,a.idCond,a.valCond,a.valPEN,a.monCond,ifnull(a.valMigo,0) valMigo,ifnull(a.valMiro,0) valMiro,articuloID,b.txtCond from tt_provision_oc a inner join tt_condicion b on a.idCond = b.idCond  where a.OrdenNro in (". substr($new_sql,1).");";
			//doEcho($new_sql);
			$res = $mysqli->query($new_sql);
			if ($res){
			
					while($row=$res->fetch_assoc()){
					$mon_pen = $row['valMigo'] - $row['valMiro'];
					
					if ($row['monCond']!=$moneda || $mon_pen <= 0){
					continue;
					}
					?>
					  <tr class="trCst">
			 <td><input type="checkbox" checked='checked' class="clsFacChk" onchange="calcDetFac()" /></td>
			 <td><a href="#" onclick="verOC(this)"><?php echo $row['OrdenNro']; ?></a></td>
			 <td><?php echo $row['OrdenPos']; ?></td>
			<td><span class="clsCod" value='<?php echo $row['idCond'];?>'><?php echo $row['articuloID'];?><span></td>
			<td class="clsTxtPos"><?php echo $row['txtCond'];?></td>
			<td class="qt clsFcPru"></td>
			<td class="qt clsFcMon"><?php echo $row['monCond']; ?></td>
            <td></td>
<!-- 			<td class="qt clsFcDct"></td>        -->
			<td class="qt"><input type="text" onchange="mr(this);calcDetFac();" value="<?php echo $mon_pen ;?>"  class="qt clsFcNet"/></td>
			<td></td>       
			</tr>
            <?php 
					}
					$res->close();
					
				$mysqli->check_result();
					
			}
		
			}
		
		
	}

}
elseif ($migo == 'X'){


	$sql = "call sp_getDocXMigo('".$cbo_tipreg."','".$t_tipreg."');";
	$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
//	doEcho($sql);
	if ($res){		
		while($row = $res->fetch_assoc()){
		$qt_pen = $row['DetOrdQtp']-$row['DetOrdQta'];  // Mostrar solo la ctd Pendiente
	?>
		  <tr>
			 <td><input type="checkbox" checked='checked' class="clsFacChk" /></td>
			 <td><a href="#" onclick="verOC(this)"><?php echo $row['OrdenNro']; ?></a></td>
			 <td><?php echo $row['OrdenPos']; ?></td>
			<td><?php echo $row['ArticuloID']; ?></td>
			<td class="clsTxtPos"><?php echo $row['DetOrdTxt']; ?></td>
			<td class="qt clsFcPru"><?php echo myRound($row['DetPreUM'] * (1 - $row['DetOrdDct'] * 0.01)) ;?></td>
            <td class="clsMon"><?php echo $row['MoneyID']; ?></td>
			<td><input class="qt clsQtPen" type="text"  onchange="calcDetFac()" value="<?php echo $qt_pen ;?>" /></td>
			<!-- <td class="qt clsFcDct"><?php echo $row['DetOrdDct'] ;?></td>         -->
            <td class="clsUM"><?php echo $row['UniMedID'];?></td>
			<td class="qt clsFcNet"><?php echo $row['DetPreNet'] ;?></td>
			<td><select class="clsFacAlm">
			<?php //echo $combo;
			echo str_replace("value='".$row['AlmacenID']."'","value='".$row['AlmacenID']."' selected='selected'",$combo); ?>
			</select></td>       
			</tr>
	<?php 	
		}
	$res->close();
		
			$mysqli->check_result();
	}


}
?>